Imports System.Math

Public Class diceRoll
    Private _numberDice As Integer = 0
    Private _numberSidesPerDice As Integer = 0
    Private _modifier As Integer = 0
    Private _modIsPositive As Boolean = True

    Public Property NumberOfDice() As Integer
        Get
            Return _numberDice
        End Get
        Set(ByVal value As Integer)
            If value < 0 Then value = 0
            _numberDice = value
            If value > 0 Then
                If NumberOfSidesPerDice < 2 Then
                    NumberOfSidesPerDice = 2
                End If
            Else
                If NumberOfSidesPerDice <> 0 Then
                    NumberOfSidesPerDice = 0
                End If
            End If
        End Set
    End Property

    Public Property NumberOfSidesPerDice() As Integer
        Get
            Return _numberSidesPerDice
        End Get
        Set(ByVal value As Integer)
            If value < 0 Then value = 0
            If value = 1 Then value = 2
            _numberSidesPerDice = value
            If value > 0 Then
                If NumberOfDice < 1 Then
                    NumberOfDice = 1
                End If
            End If
            If value = 0 Then
                If NumberOfDice <> 0 Then
                    NumberOfDice = 0
                End If
            End If

        End Set
    End Property

    Public Property Modifier() As Integer
        Get
            Return _modifier
        End Get
        Set(ByVal value As Integer)
            If value < 0 Then
                _modIsPositive = False
            Else
                _modIsPositive = True
            End If
            _modifier = value
        End Set
    End Property

    Public Shadows ReadOnly Property ToString() As String
        Get
            Dim result As String = ""
            If NumberOfDice <> 0 Then
                result = Trim(Str(NumberOfDice)) & "d" & Trim(Str(NumberOfSidesPerDice))
                If Modifier <> 0 Then
                    If _modIsPositive = True Then
                        result &= "+"
                    End If
                    result &= Trim(Str(Modifier))
                End If
            Else
                result &= Trim(Str(Modifier))
            End If
            Return result
        End Get
    End Property

    Public WriteOnly Property FromString() As String
        Set(ByVal value As String)
            Dim tmp As String = ""
            Dim modType As String = ""
            If value.Contains("d") = True Then
                tmp = Trim(Field(value, "d"c, 1))
                If IsNumeric(tmp) = True Then
                    NumberOfDice = CInt(Val(tmp))
                End If
                tmp = Trim(Field(value, "d"c, 2))
                If tmp.Contains("+") = True Then
                    modType = "+"
                End If
                If tmp.Contains("-") = True Then
                    modType = "-"
                End If
                If modType = "" Then
                    If IsNumeric(tmp) = True Then
                        NumberOfSidesPerDice = CInt(Val(tmp))
                    End If
                Else
                    Dim bit As String = Trim(Field(tmp, CChar(modType), 1))
                    If IsNumeric(bit) = True Then
                        NumberOfSidesPerDice = CInt(Val(bit))
                    End If
                    bit = Trim(Field(tmp, CChar(modType), 2))
                    If IsNumeric(bit) = True Then
                        Modifier = CInt(Val(modType & bit))
                    End If
                End If
            Else
                NumberOfDice = 0
                NumberOfSidesPerDice = 0
                If IsNumeric(value) = True Then
                    Modifier = CInt(Val(value))
                Else
                    Modifier = 0
                End If
            End If
        End Set
    End Property

    Public ReadOnly Property Range() As String
        Get
            Dim min As Integer
            Dim max As Integer
            min = NumberOfDice + Modifier
            max = (NumberOfDice * NumberOfSidesPerDice) + Modifier
            Return "From " & Str(min) & " to " & Str(max)
        End Get
    End Property

    Public Function Roll() As Integer
        Dim lp As Integer
        Dim total As Integer = 0
        For lp = 1 To _numberDice
            total = total + CInt(Int((_numberSidesPerDice * Rnd()) + 1))
        Next
        Return total + _modifier
    End Function

End Class